package com.yunshipei.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.base.BaseIntentBuilder;
import com.yunshipei.common.Globals;
import com.yunshipei.core.common.Callback;
import com.yunshipei.core.utils.YspLogUtils;
import com.yunshipei.enterplorer.R;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.model.UserInfo;
import com.yunshipei.ui.dialog.DialogHelper;
import com.yunshipei.ui.dialog.WaitDialog;
import com.yunshipei.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DeveloperActivity extends BaseActivity {

    private static final String EXTRA_USER_INFO = "ysp_extra_user_info";

    @Bind(R.id.cb_close_2_web_view)
    protected CheckBox cbClose2WebView;

    private UserInfo mUserInfo;
    private String mCompanyName = "";
    private SharedPreferences mPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_developer);
        ButterKnife.bind(this);
        mUserInfo = (UserInfo) getIntent().getSerializableExtra(EXTRA_USER_INFO);
        mCompanyName = getSharedPreferences(Globals.NO_CLEAR_SHP, MODE_PRIVATE).getString(Globals.SP_COMPANY_HOST, "");
        mPreferences = YspPreferences.getInstance().getSharedPreferences();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            cbClose2WebView.setVisibility(View.GONE);
        } else {
            boolean is2WebView = mPreferences.getBoolean(Globals.YSP_DEBUG_WEB_VIEW_MODE, true);
            cbClose2WebView.setChecked(!is2WebView);
            cbClose2WebView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mPreferences.edit().putBoolean(Globals.YSP_DEBUG_WEB_VIEW_MODE, !isChecked).apply();
                }
            });
        }
    }

    @OnClick({R.id.btn_send_log, R.id.btn_global_rt_settings, R.id.btn_preview_log})
    protected void onSubmit(View view) {
        switch (view.getId()) {
            case R.id.btn_send_log:
                final WaitDialog waitDialog = DialogHelper.getNoCancelableWaitDialog(this, "正在上传日志...");
                waitDialog.show();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.CHINA);
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String time = formatter.format(curDate);
                String fileName = mCompanyName + "+" + mUserInfo.getTel() + "+" + time + ".log";
                YspLogUtils.uploadLog(fileName, new Callback() {
                    @Override
                    public void success() {
                        waitDialog.dismiss();
                        ToastUtils.showToast("日志上传完成...");
                    }

                    @Override
                    public void error(String errorMsg) {
                        waitDialog.dismiss();
                        ToastUtils.showToast("日志上传失败，错误：" + errorMsg);
                    }
                });
                break;
            case R.id.btn_global_rt_settings:
                startActivity(new GlobalRTSettingsActivity.GlobalRTSettingsActivityIntentBuilder(this).getIntent());
                break;
            case R.id.btn_preview_log:
                startActivity(new Intent(this, PreviewLogActivity.class));
                break;
        }
    }

    public static class DeveloperIntentBuilder extends BaseIntentBuilder {

        public DeveloperIntentBuilder(Context context) {
            super(context);
        }

        public DeveloperIntentBuilder setUserInfo(UserInfo userInfo) {
            getIntent().putExtra(EXTRA_USER_INFO, userInfo);
            return this;
        }

        @Override
        protected Class<?> getTargetClazz() {
            return DeveloperActivity.class;
        }
    }
}
